package com.sxhuayuan.parking.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSON;
import com.sxhuayuan.parking.service.PropService;
import com.sxhuayuan.parking.service.RedisService;

@Service("redisPropServiceImpl")
public class RedisPropServiceImpl implements PropService {

	@Resource(name = "redisServiceImpl")
	RedisService redisService;

	final String keyTpl = "%s-%s";

	@Override
	public String getProp(String id, String name) {
		return redisService.getString(String.format(keyTpl, name, id));
	}

	@Override
	public void setProp(String id, String name, String value) {
		redisService.updateString(String.format(keyTpl, name, id), value);
	}

	@Override
	public <T> void setJsonProp(String id, String name, T t) {
		Assert.notNull(t, "");
		setProp(id, name, JSON.toJSONString(t));
	}

	@Override
	public <T> T getJsonProp(String id, String name, Class<T> cls) {
		return JSON.parseObject(getProp(id, name), cls);
	}

	@Override
	public <T> List<T> getJsonPropList(String id, String name, Class<T> cls) {
		return JSON.parseArray(getProp(id, name), cls);
	}
}
